
// 读取芯片内容(读取的时候ce oe全部拉低，改变地址就可以输出)
void read29() {
  uint32_t addr = address;
  uint32_t count = buff;
  //加上y参数的扩展地址
  read_mode();
  P_WE = 1;
  P_OE = 0;
  P_CE = 0;

  for(uint16_t i=0;i<buff;i++){
    Serial.write(getByte(address+i) & 0xff);
  }

  address += buff;
  
  P_WE = 1;
  P_OE = 1;
  P_CE = 1;
}


void write29() {

  uint16_t i = 0;
  
  // 写入是否出错
  int has_error = 0;
  
  uint32_t addr = address;
  // delayMicroseconds(1);
  for (i = 0; i < buff; i++) {
    while (!Serial.available());
      pageBuffer[i] = Serial.read();
  }

  write_mode();
  P_WE = 1;
  P_OE = 1;
  P_CE = 0;

  // delayMicroseconds(1);
  delay110ns;

  i = 0;

  // 多字节page写入
  if(page>1){
    setByte (0x5555, 0xaa);
    setByte (0x2aaa, 0x55);
    setByte (0x5555, 0xa0);
    for (i = 0; i < buff; i++) {
      setByte (addr + i, pageBuffer[i]);
      // delayMicroseconds(1);
      // delay55ns;
      // delay110ns;
    }
    P_WE = 1;
    P_OE = 1;
    P_CE = 1;
    delay(1);
    
    P_CE = 0;
    do {
      P_OE = 0;  //OE
      delayMicroseconds(2);
      // delay(1);
      // delay110ns;
      write8 = read_data_bus();
      P_OE = 1;  //OE
    }while(write8 != pageBuffer[i] && b32++ < delay_count);
    P_CE = 1;

    // P_CE = 0;
    // P_OE = 0;  //OE
    // while ((read_data_bus() != pageBuffer[i]) && b32++ < delay_count) { delayMicroseconds(1); };
    // P_OE = 1;  //OE
    // P_CE = 1;
    
    // 延时写入失败
    if (b32 >= delay_count) {
      has_error = 1;
    }
    
    if (has_error==1) {
        Serial.print("error");
    }else{
        Serial.println("ok");
    }

    //check_buff29();
  }else{
    for (i = 0; i < buff; i++) {
      write_mode();
      setByte (0x5555, 0xaa);
      setByte (0x2aaa, 0x55);
      setByte (0x5555, 0xa0);
      setByte (addr + i, pageBuffer[i]);
      // delayMicroseconds(1);
      //delayMicroseconds(10);//需要加延时
      
      delay110ns;
      
      read_mode();

      b32 = 0;
      
      P_CE = 0;
      do {
        P_OE = 0;  //OE
        // delayMicroseconds(1);
        delay110ns;
        write8 = read_data_bus();
        P_OE = 1;  //OE
      }while(write8 != pageBuffer[i] && b32++ < delay_count);
      P_CE = 1;

      // P_CE = 0;
      // P_OE = 0;  //OE
      // while ((read_data_bus() != pageBuffer[i]) && b32++ < delay_count) { delayMicroseconds(1); };
      // P_OE = 1;  //OE
      // P_CE = 1;
      
      // 延时写入失败
      if (b32 >= delay_count) {
        has_error = 1;
      }

    }
    P_WE = 1;
    P_OE = 1;
    P_CE = 1;
    //delayMicroseconds(10);

    if (has_error==1) {
        Serial.print("error");
    }else{
        Serial.println("ok");
    }

  }
  
  
}


void check_buff29() {
  uint32_t addr = address;
  read_mode();
  //读数据校验
  P_WE = 1;
  P_OE = 0;
  P_CE = 0;
  delayMicroseconds(10);
  int has_error = 0;
  for (int i = 0; i < buff ; i++) {
    uint8_t rd = getByte(addr + i);
    if (pageBuffer[i] != rd) {
      Serial.print("0x");
      Serial.print(addr + i, HEX);
      Serial.print("=");
      Serial.print(rd, HEX);
      Serial.print(",not:");
      Serial.print(pageBuffer[i], HEX);
      Serial.print(" ");
      has_error = 1;
    }
  }
  if (has_error) {
      Serial.print("error");
  }else{
      Serial.println("ok");
  }
  P_WE = 1;
  P_OE = 1;
  P_CE = 1;
}


// 检查数据是否全是 FF
void checkFF(){
  checkFF_count = 0;
  read_mode();
  
  P_WE = 1;
  P_OE = 1;
  P_CE = 0;

  for(int i = 0; i < buff; i++){
    P_OE = 0;
    b8 = getByte(address + i);
    P_OE = 1;
    if (b8 != 0xff){
      P_CE = 1;
      checkFF_count++;
    }
  }

  P_OE = 1;
  P_CE = 1;
}



